# 
# To change this template, choose Tools | Templates
# and open the template in the editor.
 

class Pagination
  
  def initialize(obj, page_size = 10, sort = nil, sort_order = 'a')
    @data = Array.new(obj)
    @page_size = page_size
    @current_page_index = 0
    @sort = sort
    @sort_order = sort_order
    @current_page_count = 1
  end
  
  def get_next_page_index
    @current_page_index + @page_size
  end
  
  def get_previous_page_index
    @current_page_index - @page_size
  end
  
  def get_next_page
    @current_page_index = get_next_page_index
    @current_page_count += 1 
    paginate(@current_page_index, @page_size)
  end
  
  def get_previous_page
    @current_page_index = get_previous_page_index
    @current_page_count -= 1
    paginate(@current_page_index, @page_size)
  end
  
  def get_total_page_count
    ((@data.size % @page_size) == 0) ? (@data.size / @page_size) : ((@data.size / @page_size) + 1)
  end

  def get_page(index)
    if index <= get_total_page_count
      @current_page_index = @page_size * (index - 1)
      @current_page_count = index
      paginate(@current_page_index, @page_size)
    end
  end
  
  def generate_page_links
    links = []
    if(@current_page_count == 1 or @current_page_count == 2)
      for i in 1...5
        links << link_to(i, get_page(i))
      end
    else
      for i in -2...3
        links << link_to((@current_page_count + i), get_page(@current_page_count + i)) if(@current_page_count + i) < get_total_page_count
      end
    end
    links << link_to(get_total_page_count, get_page(get_total_page_count))
    links
  end
  
    #~ def generate_page_links
    #~ links = []
    #~ if(@current_page_count == 1 or @current_page_count == 2)
      #~ for i in 1...6
        #~ links << "<a href='get_page(#{i})>#{i}</a>'" 
      #~ end
    #~ else
      #~ for i in -2...3
        #~ links << "<a href='get_page(#{@current_page_count + i})>#{@current_page_count + i}</a>'" if(@current_page_count + i) < get_total_page_count
      #~ end
    #~ end
    #~ links << "<a href='get_page(#{get_total_page_count})>#{get_total_page_count}</a>'" 
    #~ links
  #~ end
  
  def generate_next_previous_page_links
    links = []
    links << link_to(get_next_page_index, get_next_page)
    links << ' | '
    links << link_to(get_previous_page_index, get_previous_page)
    links
  end
  
  #~ def generate_next_previous_page_links
    #~ links = []
    #~ links << "<a href='get_next_page'>#{get_next_page_index}</a>"
    #~ links << ' | '
    #~ links << "<a href='get_previous_page'>#{get_previous_page_index}</a>"
    #~ links.join
  #~ end
  
  def paginate(start_index = 0, no_of_results_per_page = 10, sort = false, sort_order = 'a')
    page_entries = []
    @data = sort_objects(@data,sort) if sort != false
    @data.reverse! if sort!= false and sort_order.to_s == ('d')
    no_of_results_per_page.times do |cnt|
      page_entries << @data[start_index + cnt]
    end
    page_entries
  end
  
  attr_reader  :data, :page_size, :current_page_index, :sort, :sort_order, :current_page_count
end

a =[]
100.times do |i|
  a << i
end
paginate = Pagination.new(a)

#~ puts '-------- 1 --------'
#~ puts paginate.paginate
#~ puts '-------- 2 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 3 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 4 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 5 --------'
#~ puts paginate.get_next_page
#~ puts '-------- 4 -------- Previous Page'
#~ puts paginate.get_previous_page

#~ puts '-------- 5 -------- Get Page 5'
#~ puts paginate.get_page(11)

#~ puts paginate.get_total_page_count
paginate.get_page(10)
puts paginate.current_page_index
puts paginate.generate_page_links
#~ puts paginate.generate_next_previous_page_links